<?php
declare (strict_types = 1);

namespace app\admin\controller\admin;

use think\facade\Request;
use app\common\service\AdminZavote as S;
use app\common\model\AdminZavote as M;
use think\facade\Session;
use think\facade\Db;
use think\facade\View;
class Zavote extends  \app\admin\controller\Base
{
    protected $middleware = ['AdminCheck','AdminPermission'];

    // 列表
    public function index(){
        if (Request::isAjax()) {
            return $this->getJson(M::getList());
        }
        $uid = Session::get('admin.id');
        //用户权限
        if ($uid == 1) {
            $shifouchaoshi = 0;//还在时间内
            $shifouchaogeshu = 0;//没超个数限制
        }else{
            $w['id']=$uid;
            $r = Db::name('admin_admin')->where($w)->find();
            $nowtime = time();
            if($nowtime > strtotime($r['youxiaoshijian'])){
                $shifouchaoshi = 1;//超过设置时间
            }else{
                $shifouchaoshi = 0;//还在时间内
            }

            $wh['yhid']=$uid;
            $re = Db::name("admin_zavote")->where($wh)->count();
            if($re < $r['xianzhigeshu']){
                $shifouchaogeshu = 0;//没超个数限制
            }else{
                $shifouchaogeshu = 1;//超过个数限制
            }
        }
        View::assign('shifouchaoshi', $shifouchaoshi);
        View::assign('shifouchaogeshu', $shifouchaogeshu);
        return $this->fetch();
    }

    // 添加
    public function add(){
        if (Request::isAjax()) {
            return $this->getJson(S::goAdd(Request::post()));
        }
        return $this->fetch();
    }

    // 编辑
    public function edit($id){
        if (Request::isAjax()) {
            return $this->getJson(S::goEdit(Request::post(),$id));
        }
        return $this->fetch('',['model' => M::find($id)]);
    }

    // 状态
    public function status($id){
        return $this->getJson(S::goStatus(Request::post('status'),$id));
        }

    // 删除
    public function remove($id){
        return $this->getJson(S::goRemove($id));
        }

    // 批量删除
    public function batchRemove(){
        return $this->getJson(S::goBatchRemove(Request::post('ids')));
        }

    // 回收站
    public function recycle(){
        if (Request::isAjax()) {
            return $this->getJson(S::goRecycle());
        }
        return $this->fetch();
    }

    // 回收站恢复
    public function recyclehuifu($id){
        return $this->getJson(S::goRecyclehuifu($id));
    }

    // 统计票
    public function numpiao(){
        return S::goNumpiao();
    }
    // 属性
    public function shuxing(){
        return S::goShuxing();
    }
    // 状态
    public function state($id){
        return S::goState($id);
    }
    // 公众号配置
    public function zhengzhpz($id){
        return S::goZhengzhpz($id);
    }

}
